require "config"
require "TcpServer"
require 'task'
----------------------------
require "logging"
logger = logging.new("%date.log",30);

----------------------------------
--  System Function
----------------------------------

Send = function(fd,msg,len)
	TcpServer.Send(fd,msg,len);
end
Disconnect = TcpServer.Disconnect;

local stoped = false;
local tskMain = 1;
IsStopping = function()
	local buf, tskSender, rc = task.receive (0);
	if buf == 'quit' then
		tskMain = tskSender;
		stoped = true;
	end
	return stoped;
end

function Shutdown()
	print("Shutdown");
end

--------------------------------
-- Start
--------------------------------
logger:info("Service Start");

Initialize();
TcpServer.Run(GetPorts(),OnConnect,OnRecv,OnDisconnect,IsStopping);

logger:info("Service Stopping");
Release();
logger:info("Service Stop");

task.post ( tskMain , 'stop ok', 0)
